/**
 * 附件上传下载Mixins
 */

import {downloadFile,filedownload,UploadFile} from "@/api/system";
import {fileUploadMaps} from "@/views/review/const"
const fileTypeMixin = {
  data() {
    return {
      // 附件类型id存放
      fileTypeIds:{},
    }
  },
  methods:{
    // 下载文件
    downloadMixin (data,filename) {
      if (data==null) {
        return
      }else{
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download',filename )
        document.body.appendChild(link)
        link.click()
      }
    },
    // 附件预览
    fileDownload(id,fileName){
      filedownload(id).then(response => {
        if(response.status==200){
          this.downloadMixin(response.data,fileName)
        }
      }).catch((error) => {

      })
    },
    // 上传失败弹框
    failNotify(){
      this.$notify({
        title: "失败",
        message: "上传失败",
        type: "error",
        duration: 2000
      });
    },
    // 上传成功弹框
    successNotify(){
      this.$notify({
        title: "成功",
        message: "上传成功",
        type: "success",
        duration: 2000
      });
    },
    // 附件上传方法调用（成功失败）-》多态
    fileUploadCallMethod(map,type,res,param,self){
      switch (type) {
        case 'success':
          if(map.sucMethod instanceof Function){
            map.sucMethod(res,param,self)
          }
          break;
        case 'fail':
          if(map.failMethod instanceof Function){
            map.failMethod(res,param,self)
          }
          break
        default:
          break
      }
    },
    // 附件上传
    fileUpload(param,fileType,map) {
      const formData = new FormData()
      formData.append(param.file.uid, param.file)
      UploadFile(formData,{fileType:fileType},param).then(response => {
        if (response.data.code == 0) {
          this.fileUploadCallMethod(map,'success',response,param,this)
        }
      }).catch(response => {
        this.fileUploadCallMethod(map,'fail',response,param,this)
      })
    },
    // 获取附件Ids
    getFileIds(fileList){
      var fileIds = [];
      for(let i in fileList){
        fileIds.push(fileList[i].id)
      }
      return fileIds;
    },
    // 获取单个附件ID
    getFileId(fileList){
      if(fileList && fileList[0]){
        return fileList[0].id
      }
      return ''
    },
    // 材料清单附件上传
    fileUploadRequest(params){
      this.fileUpload(params,'3',fileUploadMaps(this.fileType,this.successCallback,this.failCallback))
    },
    // 材料清单附件上传
    fileUploadRequestName(params,name){
      this.fileUpload(params,'3',fileUploadMaps(name,this.successCallback,this.failCallback))
    },
    // 材料清单上传成功callback 根据页面业务重写
    successCallback(){},
    // 材料清单上传失败callback 根据页面业务重写
    failCallback(){},
    // 材料清单附件预览
    fileHandlePreview(file){
      var curFile = this.fileTypeIds[this.fileType].filter(item => item.uid == file.uid)
      // 修改时默认附件没有uid
      if(!curFile){
        curFile = this.fileTypeIds[this.fileType].filter(item => item.id == file.id)
      }
      var id = ''
      if(curFile && curFile.length>0){
        id = curFile[0].id
      }
      this.fileDownload(id,file.name)
    },
    // 材料清单附件移除
    fileHandleRemove(file){
      this.fileTypeIds[this.fileType] = this.fileTypeIds[this.fileType].filter(item => item.uid != file.uid)
      // 修改时默认附件没有uid
      if(file.id){
        this.fileTypeIds[this.fileType] = this.fileTypeIds[this.fileType].filter(item => item.id != file.id)
      }
      if(typeof this.removeCallback === 'function'){
        this.removeCallback()
      }
    },
    fileHandlePreviewName(file,name){
      var curFile = this.fileTypeIds[name].filter(item => item.uid == file.uid)
      // 修改时默认附件没有uid
      if(!curFile){
        curFile = this.fileTypeIds[name].filter(item => item.id == file.id)
      }
      var id = ''
      if(curFile && curFile.length>0){
        id = curFile[0].id
      }
      this.fileDownload(id,file.name)
    },
    // 材料清单附件移除
    fileHandleRemoveName(file,name){
      this.fileTypeIds[name] = this.fileTypeIds[name].filter(item => item.uid != file.uid)
      // 修改时默认附件没有uid
      if(file.id){
        this.fileTypeIds[name] = this.fileTypeIds[name].filter(item => item.id != file.id)
      }
      if(typeof this.removeCallback === 'function'){
        this.removeCallback()
      }
    },
    // 附件移除callback 根据页面业务重写
    removeCallback(){}
  }
};

export default fileTypeMixin;
